package com.ds.infra.rpc.core.protocol;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;

import java.io.Serial;
import java.io.Serializable;

/**
 * RPC响应体定义
 *
 * @author WeiShaoying
 */
@Data
public class RpcResponse implements Serializable {

    @Serial
    private static final long serialVersionUID = 5800691968605403420L;
    /**
     * 对应请求ID
     */
    private String requestId;

    /**
     * 调用结果（成功时返回）
     */
    private Object data;

    /**
     * 错误信息（失败时返回）
     */
    private String errorMsg;

    /**
     * 响应状态（自定义状态码）
     */
    private int status = ResponseStatus.SUCCESS;

    // 显式忽略未知字段
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class ResponseWrapper {
        private RpcResponse response;
    }

    /**
     * 响应状态枚举
     */
    public interface ResponseStatus {
        int SUCCESS = 200;
        int SERVICE_NOT_FOUND = 404;
        int SERVER_ERROR = 500;
        int TIMEOUT = 504;
    }

    public boolean isSuccess() {
        return status == ResponseStatus.SUCCESS;
    }

    @Override
    public String toString() {
        return "RpcResponse{" +
                "requestId='" + requestId + '\'' +
                ", status=" + status +
                (isSuccess() ?
                        ", data=" + data :
                        ", errorMsg='" + errorMsg + '\'') +
                '}';
    }
}
